package com.cn.mmd.controller;


import com.cn.mmd.model.Information;
import com.cn.mmd.model.TeacherData;
import com.cn.mmd.service.InformationService;
import com.cn.mmd.tools.GetLimitNum;
import com.sun.tools.internal.ws.processor.model.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 上传资料
 */
@Controller
@RequestMapping(value = "page/information")
public class InformationController {

    @Autowired
    private InformationService informationService;

    private GetLimitNum getLimitNum = new GetLimitNum();

    @Autowired
    private TokenController tokenController;

    @RequestMapping(value = "/add")//    @RequiresPermissions("user:create")
    public synchronized
    @ResponseBody
    Object informationAdd(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file) {
        Map<String, Object> map = new HashMap<String, Object>();
        String publisherid = request.getParameter("publisherId");
        String informationname = request.getParameter("informationName");
        String informationcontent = request.getParameter("informationContent");
        String jsonclass = request.getParameter("jsonClass");
        String token= request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(publisherid,token);
        if (teacherData!=null) {
            String path = request.getSession().getServletContext().getRealPath("/") + "upload";
            String fileName = file.getOriginalFilename();
            File targetFile;
            String url;
            if (file.getOriginalFilename().equals("")) {
                url = "";
                targetFile = null;
            } else {
                url = "";
                try {
                    url = InetAddress.getLocalHost().getHostAddress() + ":" + request.getLocalPort() + "/" + request.getContextPath() + "/upload/" + publisherid + "-information-" + fileName;
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                targetFile = new File(path, publisherid + "-information-" + fileName);
            }
            Date date = new Date();
            SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
            String releasetime = time.format(date);
            Information information = new Information(url, publisherid, releasetime, jsonclass, informationname, informationcontent);
            Information information1 = new Information(url, publisherid, jsonclass, informationname, informationcontent);
            information1 = informationService.selectByInformationAll(information1);
            if (information1 == null) {
                if (informationService.insertSelective(information) > 0) {
                    if (targetFile != null) {
                        if (!targetFile.exists()) {
                            targetFile.mkdirs();
                            try {
                                file.transferTo(targetFile);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            targetFile.delete();
                            try {
                                file.transferTo(targetFile);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                    map.put("code", 200);
                    map.put("message", "成功");
                } else {
                    map.put("code", 201);
                    map.put("message", "失败");
                }
            } else {
                information = new Information(url, publisherid, releasetime, jsonclass, informationname, informationcontent, information1.getInformationid());
                if (informationService.updateByPrimaryKeySelective(information) > 0) {
                    if (targetFile != null) {
                        if (!targetFile.exists()) {
                            targetFile.mkdirs();
                            try {
                                file.transferTo(targetFile);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            targetFile.delete();
                            try {
                                file.transferTo(targetFile);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        map.put("code", 200);
                        map.put("message", "成功");
                    } else {
                        map.put("code", 201);
                        map.put("message", "失败");
                    }
                }
            }
        }else {
            map.put("code", 202);
            map.put("message", "此用户没有权限做此操作");
        }
        return map;
    }


    @RequestMapping(value = "/select")
    public synchronized
    @ResponseBody
    Object informationSelect(HttpServletRequest request ) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        Map<String, Object> map;
        Integer startrow = Integer.parseInt(request.getParameter("startrow"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
//        model.addAttribute("informationName",informationName);
        String informationName = request.getParameter("informationName");
        String teacherId = request.getParameter("teacherId");
        String token= request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId,token);
        if (teacherData!=null) {
            startrow = (startrow - 1) * pageSize;
            Information information = new Information(startrow, pageSize, informationName);
            List<Information> informationList = informationService.selectLimit(information);
            for (int i = 0; i < informationList.size(); i++) {
                information = informationList.get(i);
                map = new HashMap<String, Object>();
                System.out.println(information.getInformationname());
                map.put("informationId", information.getInformationid());
                map.put("informationName", information.getInformationname());
                map.put("releaseTime", information.getReleasetime());
                map.put("readNumber", information.getReadnumber() + "");
                mapList.add(map);
            }

            if (mapList != null && mapList.size() > 0) {
                information = new Information();
                information.setInformationname(informationName);
                int i1 = informationService.countLimitNum(information);
                objectMap.put("total", i1);
                objectMap.put("data", mapList);
                objectMap.put("code", 200);
                objectMap.put("message", "成功");
            } else {
                information = new Information();
                information.setInformationname(informationName);
                int i1 = informationService.countLimitNum(information);
                objectMap.put("total", i1);
                objectMap.put("code", 201);
                objectMap.put("message", "失败没有数据");
            }
        }else {
            objectMap.put("code", 202);
            objectMap.put("message", "此用户没有权限做此操作");
        }
        return objectMap;
    }
}


//JSONArray jsonArray = JSONArray.fromObject(jsonclass);
//        for(int i=0;i<jsonArray.size();i++){
//            JSONObject jsonobject2=jsonArray.getJSONObject(i);
//            int value1=jsonobject2.getInt("hello");
//            boolean value2=jsonobject2.getBoolean("abc");
////          String value3=jsonobject2.getString("xyz");
//            JSONObject jsonobject3=jsonobject2.getJSONObject("xyz");
//            int value4=jsonobject3.getInt("a");
//            String value5=jsonobject3.getString("b");
//            System.out.println(value1);
//            System.out.println(value2);
//            System.out.println(value4);
//            System.out.println(value5);
//        }